package com.example.phototest.Controller;

import com.example.phototest.Mapper.photoMapper;
import com.example.phototest.Service.photoService;
import com.example.phototest.Utils.PhotoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/photo")

public class photoController {
    @Autowired
    private photoService photoService;
    @RequestMapping("/get")
    public String getPhoto( @RequestPart("jpgfile") MultipartFile file,
                            @RequestParam(value = "username",required = true) String username) throws IOException {
        // 获取⽂件后缀名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "")+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        System.out.println(fileName);
        System.out.println(username);
        // ⽂件保存地址
        String filePath = "/Users/mamba24/Desktop/code/"+fileName;
        System.out.println(filePath);
        file.transferTo(new File(filePath));
        int ret=photoService.addURL(filePath);
        return "success"+ret;
    }


//    @RequestMapping("upload")
//    public Object getImg(){
//        //根据自己的返回格式返回就行
//        return photoService.personImg();
//    }

    @RequestMapping("/upload")
    public void uploadPhoto(HttpServletResponse response) throws IOException {
        PhotoUtil util=new PhotoUtil();
        Image image = ImageIO.read(new FileInputStream("/Users/mamba24/Desktop/code/e8c49417014243639af40ec7bafa1b84.jpg"));
        util.responseImage(image,response);
    }


}
